package ipl.projet.servlets;

import ipl.projet.domaine.Partie;
import ipl.projet.exceptions.EtatException;
import ipl.projet.exceptions.PartieInconnueException;
import ipl.projet.exceptions.UtilisateurDuplique;
import ipl.projet.usecase.UccStarTrek;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Accueil extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;

	@EJB
	private UccStarTrek ucc;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession sessionTEST = request.getSession();
		if (sessionTEST == null || sessionTEST.getAttribute("login") == null) {
			response.sendRedirect(response
					.encodeRedirectURL("connexion.html?erreur=1"));
			return;
		}

		HttpSession session = request.getSession();
		String login = (String) session.getAttribute("login");

		if (request.getParameter("rejoindrePartie") != null) {
			String urlErreur = "";
			try {
				ucc.rejoindrePartie(request.getParameter("rejoindrePartie"),
						login);
			} catch (UtilisateurDuplique e) {
				urlErreur = "?erreur=1";
			} catch (EtatException e) {
				urlErreur = "?erreur=2";
			} catch (PartieInconnueException e) {
				urlErreur = "?erreur=3";
			}

			if (urlErreur.isEmpty()) {
				session.setAttribute("partie",
						request.getParameter("rejoindrePartie"));
				response.sendRedirect(response
						.encodeRedirectURL("placement.html"));
				return;
			} else {
				response.sendRedirect(response.encodeRedirectURL("accueil.html"
						+ urlErreur));
				return;
			}
		}

		if (request.getParameter("visualiserJournal") != null) {
			response.sendRedirect(response
					.encodeRedirectURL("journal.html?partie="
							+ request.getParameter("visualiserJournal")));
			return;
		}

		request.setAttribute("login", login);

		List<Partie> listePartiesTerminees = ucc.listerPartiesTerminees();
		request.setAttribute("listePartiesTerminees", listePartiesTerminees);

		List<Partie> listePartiesEnAttente = ucc.listerPartiesEnAttente();
		request.setAttribute("listePartiesEnAttente", listePartiesEnAttente);

		List<Partie> listePartiesEnCours = ucc.listerPartiesEnCours();
		request.setAttribute("listePartiesEnCours", listePartiesEnCours);

		getServletContext().getNamedDispatcher("accueil.html").forward(request,
				response);
		return;
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("creerPartie") != null) {
			response.sendRedirect(response
					.encodeRedirectURL("creerPartie.html"));
			return;
		} else {

		}
	}
}
